package com.atao.baokede.controller;

import com.atao.baokede.pojo.Result;
import com.atao.baokede.service.UserService;
import com.atao.baokede.utils.CodeUtil;
import jakarta.mail.internet.MimeMessage;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

import static com.atao.baokede.constant.EmailConstant.EMAIL_FROM;

/**
 * 验证码层
 */
@RestController
@RequestMapping("/code")
public class CodeController {
    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    /**
     * 发送4位数注册验证码
     * @param email 邮箱
     * @return 无
     * @throws Exception
     */
    @GetMapping("/sendRegister")
    public Result sendRegister(@Valid @Email String email) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("宝可得 <" + EMAIL_FROM + ">");
        helper.setTo(email);
        helper.setSubject("注册验证码");
        String code = CodeUtil.generateCode(4);

        //将验证码存入Redis，并设置过期时间为5分钟,key为email,value为code
        redisTemplate.opsForValue().set("registerEmail:" + email, code, 5, TimeUnit.MINUTES);

        helper.setText(CodeUtil.getHtmlWithCode(code), true);  // true表示发送HTML格式
        mailSender.send(message);
        return Result.success();
    }


    /**
     * 发送4位数更新密码验证码
     * @param email 邮箱
     * @return  无
     * @throws Exception
     */
    @GetMapping("/sendUpdatePwd")
    public Result sendUpdatePwd(@Valid @Email String email) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("宝可得 <" + EMAIL_FROM + ">");
        helper.setTo(email);
        helper.setSubject("注册验证码");
        String code = CodeUtil.generateCode(4);
        //将验证码存入Redis，并设置过期时间为5分钟,key为email,value为code
        redisTemplate.opsForValue().set("updatePwdEmail:" + email, code, 5, TimeUnit.MINUTES);
        helper.setText(CodeUtil.getHtmlWithCode(code), true);  // true表示发送HTML格式
        mailSender.send(message);
        return Result.success();
    }


    /**
     * 发送4位数登录的验证码
     * @param email 邮箱
     * @return 无
     * @throws Exception
     */
    @GetMapping("/sendLogin")
    public Result sendLongin(@Valid @Email String email) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("宝可得 <" + EMAIL_FROM + ">");
        helper.setTo(email);
        helper.setSubject("登录验证码");
        String code = CodeUtil.generateCode(4);

        //将验证码存入Redis，并设置过期时间为5分钟,key为email,value为code
        redisTemplate.opsForValue().set("loginEmail:" + email, code, 5, TimeUnit.MINUTES);

        helper.setText(CodeUtil.getHtmlWithCode(code), true);  // true表示发送HTML格式
        mailSender.send(message);
        return Result.success();
    }
}
